package com.pr.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pr.beans.News;
import com.pr.mapper.NewsMapper;
import com.pr.service.NewsService;
import com.pr.utils.DataResults;
import com.pr.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import cn.hutool.core.date.DateUtil;
import java.util.List;


@RestController
public class NewsServiceImpl implements NewsService  {


    @Autowired
    NewsMapper newsMapper;

    @Override
    public DataResults<List<News>> list() {
        return DataResults.success(ResultCode.SUCCESS,newsMapper.selectList(
                new QueryWrapper<News>().eq("del",0)
        ));
    }

    @Override
    public DataResults insertNews(News news) {
        news.setTimes(DateUtil.now());
        return DataResults.success(ResultCode.SUCCESS,newsMapper.insert(news));
    }

    @Override
    public DataResults updateNews(News news) {
        news.setTimes(DateUtil.now());
        return DataResults.success(ResultCode.SUCCESS,newsMapper.updateById(news));
    }

    @Override
    public DataResults deleteNewsById(Integer id) {
        News news = new News(id,1);
        return DataResults.success(ResultCode.SUCCESS,newsMapper.updateById(news));
    }

    @Override
    public DataResults<News> findNewsById(Integer id) {
        return DataResults.success(ResultCode.SUCCESS,newsMapper.selectById(id));
    }
}
